package view.Tabs;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import view.Windows.ErrorMessageGUI;

import commons.Entities.LegenCharacter;
import commons.Exceptions.characterWithNoNameException;

import controller.ControllerFunctions;

public class AddCharacterGUI {

	private static final Log logger = LogFactory.getLog(AddCharacterGUI.class);

	// final public Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected boolean selectedFavorite = false;

	public AddCharacterGUI(final Display mainDisp, final Composite compMain, final CTabItem thisTab) {

		initFonts(mainDisp);
		// main window definitions
		compMain.setBounds(10, 10, 900, 700);
		setBackgroundColor(new Color(mainDisp, 240, 240, 240));
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		compMain.setForeground(clrWhite);

		// Register group
		Group grRegister = new Group(compMain, 0);
		grRegister.setBounds(50, 20, 800, 550);
		Label lblRegister = new Label(compMain, 0);
		lblRegister.setForeground(clrWhite);
		lblRegister.setText("New Character");
		lblRegister.setBounds(60, 20, 105, 30);
		lblRegister.moveAbove(grRegister);
		lblRegister.setAlignment(SWT.CENTER);

		// name
		Label lblName = new Label(grRegister, 0);
		lblName.setText("Name: ");
		lblName.setBounds(170, 40, 100, 40);
		lblName.setForeground(clrWhite);
		lblName.setFont(getLabelFont());

		final Text txName = new Text(grRegister, SWT.BORDER);
		txName.setBounds(290, 40, 360, 40);
		txName.setFont(getLabelFont());

		// characterCreatedBy
		Label lblCreatedBy = new Label(grRegister, 0);
		lblCreatedBy.setText("Created By: ");
		lblCreatedBy.setBounds(30, 100, 120, 20);
		lblCreatedBy.setForeground(clrWhite);

		final Text txCreatedBy = new Text(grRegister, SWT.BORDER);
		txCreatedBy.setBounds(170, 100, 160, 20);
		txCreatedBy.setText(HomePageGUI.get().getUser().getUserName());

		// based on
		Label lblBasedOn = new Label(grRegister, 0);
		lblBasedOn.setText("based on: ");
		lblBasedOn.setBounds(30, 130, 130, 20);
		lblBasedOn.setForeground(clrWhite);

		final Text txBasedOn = new Text(grRegister, SWT.BORDER);
		txBasedOn.setBounds(170, 130, 160, 20);

		// POB
		Label lblPOB = new Label(grRegister, 0);
		lblPOB.setText("Place Of Birth: ");
		lblPOB.setBounds(30, 160, 130, 20);
		lblPOB.setForeground(clrWhite);

		final Text txPOB = new Text(grRegister, SWT.BORDER);
		txPOB.setBounds(170, 160, 160, 20);

		// gender
		Label lblGender = new Label(grRegister, 0);
		lblGender.setText("Gender: ");
		lblGender.setBounds(30, 190, 130, 20);
		lblGender.setForeground(clrWhite);

		final Text txGender = new Text(grRegister, SWT.BORDER);
		txGender.setBounds(170, 190, 160, 20);

		// species
		Label lblSpecies = new Label(grRegister, 0);
		lblSpecies.setText("Species: ");
		lblSpecies.setBounds(30, 220, 130, 20);
		lblSpecies.setForeground(clrWhite);

		final Text txSpecies = new Text(grRegister, SWT.BORDER);
		txSpecies.setBounds(170, 220, 160, 20);

		// Parents
		Label lblParents = new Label(grRegister, 0);
		lblParents.setText("Parents: ");
		lblParents.setBounds(30, 250, 130, 20);
		lblParents.setForeground(clrWhite);

		final Text txParents = new Text(grRegister, SWT.BORDER);
		txParents.setBounds(170, 250, 160, 20);

		// Occupation
		Label lblOccupation = new Label(grRegister, 0);
		lblOccupation.setText("Occupation: ");
		lblOccupation.setBounds(30, 280, 130, 20);
		lblOccupation.setForeground(clrWhite);

		final Text txOccupation = new Text(grRegister, SWT.BORDER);
		txOccupation.setBounds(170, 280, 160, 20);

		FontData fontData = new FontData("Ariel", 10, SWT.BOLD);
		setHeadFont(new Font(mainDisp, fontData));

		// Rank
		Label lblRank = new Label(grRegister, 0);
		lblRank.setText("Rank: ");
		lblRank.setBounds(30, 310, 130, 20);
		lblRank.setForeground(clrWhite);

		final Text txRank = new Text(grRegister, SWT.BORDER);
		txRank.setBounds(170, 310, 160, 20);

		// Organizations
		Label lblOrganizations = new Label(grRegister, 0);
		lblOrganizations.setText("Organizations: ");
		lblOrganizations.setBounds(30, 340, 130, 20);
		lblOrganizations.setForeground(clrWhite);

		final Text txOrganizations = new Text(grRegister, SWT.BORDER);
		txOrganizations.setBounds(170, 340, 160, 20);

		// Powers/Abilities
		Label lblPowersOrAbilities = new Label(grRegister, 0);
		lblPowersOrAbilities.setText("Powers/Abilities: ");
		lblPowersOrAbilities.setBounds(30, 370, 130, 20);
		lblPowersOrAbilities.setForeground(clrWhite);

		final Text txPowersOrAbilities = new Text(grRegister, SWT.BORDER);
		txPowersOrAbilities.setBounds(170, 370, 160, 20);

		// interested in
		Label lblFictionalUniverses = new Label(grRegister, 0);
		lblFictionalUniverses.setText("Fictional Universes: ");
		lblFictionalUniverses.setBounds(30, 400, 130, 20);
		lblFictionalUniverses.setForeground(clrWhite);

		final Text txFictionalUniverses = new Text(grRegister, SWT.BORDER);
		txFictionalUniverses.setBounds(170, 400, 160, 20);

		// children
		Label lblChildren = new Label(grRegister, 0);
		lblChildren.setText("Children: ");
		lblChildren.setBounds(400, 100, 185, 20);
		lblChildren.setForeground(clrWhite);

		final Text txChildren = new Text(grRegister, SWT.BORDER);
		txChildren.setBounds(595, 100, 160, 20);

		// quotations
		Label lblQuotations = new Label(grRegister, 0);
		lblQuotations.setText("Quotations: ");
		lblQuotations.setBounds(400, 130, 185, 20);
		lblQuotations.setForeground(clrWhite);

		final Text txQuotations = new Text(grRegister, SWT.BORDER);
		txQuotations.setBounds(595, 130, 160, 20);

		// romanticallyInvolvedWith
		Label lblRIW = new Label(grRegister, 0);
		lblRIW.setText("Romantically Involved With: ");
		lblRIW.setBounds(400, 160, 185, 20);
		lblRIW.setForeground(clrWhite);

		Text txRIW = new Text(grRegister, SWT.BORDER);
		txRIW.setBounds(595, 160, 160, 20);

		// ethnicity
		Label lblEthnicity = new Label(grRegister, 0);
		lblEthnicity.setText("Ethnicity: ");
		lblEthnicity.setBounds(400, 190, 185, 20);
		lblEthnicity.setForeground(clrWhite);

		final Text txEthnicity = new Text(grRegister, SWT.BORDER);
		txEthnicity.setBounds(595, 190, 160, 20);

		// education
		Label lblEducation = new Label(grRegister, 0);
		lblEducation.setText("Education: ");
		lblEducation.setBounds(400, 220, 185, 20);
		lblEducation.setForeground(clrWhite);

		final Text txEducation = new Text(grRegister, SWT.BORDER);
		txEducation.setBounds(595, 220, 160, 20);

		// Parents
		Label lblHasPossessed = new Label(grRegister, 0);
		lblHasPossessed.setText("Has Possessed: ");
		lblHasPossessed.setBounds(400, 250, 185, 20);
		lblHasPossessed.setForeground(clrWhite);

		final Text txHasPossessed = new Text(grRegister, SWT.BORDER);
		txHasPossessed.setBounds(595, 250, 160, 20);

		// medicalConditions
		Label lblMedicalConditions = new Label(grRegister, 0);
		lblMedicalConditions.setText("Medical Conditions: ");
		lblMedicalConditions.setBounds(400, 280, 185, 20);
		lblMedicalConditions.setForeground(clrWhite);

		final Text txMedicalConditions = new Text(grRegister, SWT.BORDER);
		txMedicalConditions.setBounds(595, 280, 160, 20);

		// dateOfBirth
		Label lblDateOfBirth = new Label(grRegister, 0);
		lblDateOfBirth.setText("Date of Birth: ");
		lblDateOfBirth.setBounds(400, 310, 185, 20);
		lblDateOfBirth.setForeground(clrWhite);

		final Text txDateOfBirth = new Text(grRegister, SWT.BORDER);
		txDateOfBirth.setBounds(595, 310, 160, 20);

		// height
		Label lblHeight = new Label(grRegister, 0);
		lblHeight.setText("Height: ");
		lblHeight.setBounds(400, 340, 185, 20);
		lblHeight.setForeground(clrWhite);

		final Text txHeight = new Text(grRegister, SWT.BORDER);
		txHeight.setBounds(595, 340, 160, 20);

		// weight
		Label lblWeight = new Label(grRegister, 0);
		lblWeight.setText("Weight: ");
		lblWeight.setBounds(400, 370, 185, 20);
		lblWeight.setForeground(clrWhite);

		final Text txWeight = new Text(grRegister, SWT.BORDER);
		txWeight.setBounds(595, 370, 160, 20);

		// placeslived
		Label lblPlacesLived = new Label(grRegister, 0);
		lblPlacesLived.setText("Places Lived: ");
		lblPlacesLived.setBounds(400, 400, 185, 20);
		lblPlacesLived.setForeground(clrWhite);

		final Text txPlacesLived = new Text(grRegister, SWT.BORDER);
		txPlacesLived.setBounds(595, 400, 160, 20);

		// Add Character Button
		Button btnRegister = new Button(grRegister, SWT.NONE);
		btnRegister.setFont(getButtonFont());
		btnRegister.setText("Add Character");
		btnRegister.setBounds(300, 500, 200, 30);
		btnRegister.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				LegenCharacter lc = new LegenCharacter();
				lc.setCharacterName(txName.getText());
				lc.setBasedOn(txBasedOn.getText());
				lc.setPlaceOfBirth(txPOB.getText());
				lc.setGender(txGender.getText());
				lc.setParents(txParents.getText());
				lc.setSpecies(txSpecies.getText());
				lc.setOccupation(txOccupation.getText());
				lc.setRank(txRank.getText());
				lc.setOrganizations(txOrganizations.getText());
				lc.setPowersOrAbilities(txPowersOrAbilities.getText());
				lc.setAppearsInTheseFictionalUniverses(txFictionalUniverses.getText());
				lc.setChildren(txChildren.getText());
				lc.setCharacterCreatedBy(txCreatedBy.getText());
				lc.setQuotations(txQuotations.getText());
				lc.setEthnicity(txEthnicity.getText());
				lc.setEducation(txEducation.getText());
				lc.setHasPossessed(txHasPossessed.getText());
				lc.setMedicalConditions(txMedicalConditions.getText());
				lc.setDateOfBirth(txDateOfBirth.getText());
				lc.setHeight(txHeight.getText().equals("") ? 0 : Float.parseFloat(txHeight.getText()));
				lc.setWeight(txWeight.getText().equals("") ? 0 : Float.parseFloat(txWeight.getText()));
				lc.setPlacesLived(txPlacesLived.getText());

				try {
					ControllerFunctions.get().addNewCharacter(lc);
				} catch (characterWithNoNameException e1) {
					logger.info(e1);
					ErrorMessageGUI errorMsg = new ErrorMessageGUI(mainDisp, e1.toString());
					errorMsg.showGUI(mainDisp);
					return;
				}
				compMain.dispose();
				thisTab.dispose();
			}
		});
	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	// initialize fonts
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 18, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}
}
